package cn.yuhh.core.util;

import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;


public class FileUtil {

    static Log log = LogFactory.getLog(FileUtil.class);

    public static void write(String dir, String fileName, byte[] bytes) throws IOException {
        try {
            File file = new File(dir);
            if (!file.exists()) {
                FileUtils.forceMkdir(file);
            }
            File dirFile = new File(dir.concat(File.separator).concat(fileName));
            FileUtils.writeByteArrayToFile(dirFile, bytes);
        } catch (Exception e) {
            log.error(e);
        }
    }

    public static void writeLine(String dir, String fileName, String line) throws IOException {
        try {
            File file = new File(dir);
            if (!file.exists()) {
                FileUtils.forceMkdir(file);
            }
            File dirFile = new File(dir.concat(File.separator).concat(fileName));
            List<String> lines = new ArrayList<String>();
            lines.add(line);
            FileUtils.writeLines(dirFile, lines, true);
        } catch (Exception e) {
            log.error(e);
        }
    }


    public static String generateFileName() {
        return UUIDGenerator.uuid() + "_" + System.currentTimeMillis();
    }

    public static String getFileExtName(String fileName) {
        if (org.springframework.util.StringUtils.isEmpty(fileName)) {
            return null;
        }
        return fileName.substring(fileName.lastIndexOf(".") + 1);
    }

    /**
     * 获得指定文件的byte数组
     */
    public static byte[] getBytes(String filePath) {
        byte[] buffer = null;
        try {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }

    /**
     * 获得指定文件的byte数组
     */
    public static byte[] getBytes(File file) {
        byte[] buffer = null;
        try {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }

    /**
     * 根据byte数组，生成文件
     */
    public static void getFile(byte[] bfile, String filePath, String fileName) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            File dir = new File(filePath);
            if (!dir.exists() && dir.isDirectory()) {//判断文件目录是否存在
                boolean mkdirs = dir.mkdirs();
            }
            file = new File(filePath + "\\" + fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bfile);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        File[] sort = sort(new File("C:\\temp").listFiles());
        for (File file : sort) {
            System.out.println(file);
        }
    }


    public static File getParentDirectory(String filePath) {
        File file = new File(filePath);
        if (file.isDirectory()) {
            return file;
        }
        return file.getParentFile();
    }

    public static File[] sort(File[] files) {
        if (files == null || files.length == 0) {
            return files;
        }
        Arrays.sort(files, new Comparator<File>() {
            @Override
            public int compare(File o1, File o2) {
                if (o1.isDirectory() && o2.isFile())
                    return -1;
                if (o1.isFile() && o2.isDirectory())
                    return 1;
                return o1.getName().compareTo(o2.getName());
            }
        });
        return files;
    }


}
